/**
 * 
 * 中间件都是return一个async
 */

const path = require('path')
const util = require('util')
const fs = require('fs').promises

module.exports = (dirname, {
    map
}) => {
    return async (ctx, next) => {
        ctx.render = async (filename, renderObj) => {
            // 取到后缀
            let ext = Object.keys(map)[0]
            // 找到对应文件  拼出地址
            let absPath = path.join(dirname, filename) + '.' + ext
            //将ejs.renderFile 转成异步的
            let renderFile = util.promisify(require(map[ext]).renderFile);
            let template = await renderFile(absPath, renderObj)

            ctx.body = template
        }
       await next()
    }
}